package cn.dm.example.threadPool;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class ThreadPoolExample1 {

    public static void main(String[] args) {
        // newCachedThreadPool
        ExecutorService executorService = Executors.newCachedThreadPool();

        for (int i = 0; i < 10; i++) {
            final int index = i;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    log.info("task:{}", index);
                }
            });
        }
        // 注:关闭线程池代码一般写在finally代码块中,该测例仅为了方便演示
        executorService.shutdown();
        /**
         * 21:19:18.862 [pool-1-thread-8] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:7
         * 21:19:18.861 [pool-1-thread-2] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:1
         * 21:19:18.861 [pool-1-thread-9] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:8
         * 21:19:18.861 [pool-1-thread-6] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:5
         * 21:19:18.862 [pool-1-thread-10] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:9
         * 21:19:18.861 [pool-1-thread-7] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:6
         * 21:19:18.861 [pool-1-thread-3] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:2
         * 21:19:18.862 [pool-1-thread-1] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:0
         * 21:19:18.861 [pool-1-thread-4] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:3
         * 21:19:18.862 [pool-1-thread-5] INFO cn.dm.example.threadPool.ThreadPoolExample1 - task:4
         */
    }
}
